#!/bin/sh

cli_help() {
  cat << EOF

e2e [<option> ...] [<phase> ...] [-- [<test spec> ...]]

If no <test spec> is specified all the e2e tests under stackgres-k8s/e2e/spec
 will be executed.
The "<test spec>" are files that can be found under stackgres-k8s/e2e/spec but
 you may specify the file name in such folder or the relative path

Phases:

  test: run e2e tests, the if no phase is specified
  pull: pull images from repositories
  build: build operator images
  push: push operator images to repositories

Options:

  --help                  Print this help and exit
  --debug                 Print shell xtrace to debug the E2E framework
  --use-cache             Enable test cache used to store test results
  --excluded-tests <test> [<test> ...]: Specify the list of tests or test's paths to exclude from running
  --retry <number>        Specify the number of retries for failed tests. If
                           not specified, failed tests will not be retried
  --timeout <number>      Specify the timeout in seconds that the tests uses to consider an action failed.
                           It can be used to increase toleration to resource usage on some evironments.
  --parallelism <number>  Specify the parallelism that will be used for
                           running multiple e2e tests in parallel (when
                           possible) and other operations
  --env <environment>     Allow to use one of the k8s environment present in
                           the folder stackgres-k8s/e2e/envs
  --reset-env             Specify that the k8s environment have to be
                           recreated if it already exists
  --native-image-tag      If set use the native image tag for the operator images
  --image-tag <tag>       Specify the JVM image tag to use for the operator
                           images
  --use-latest-image-tag  If default or specified image tag is not found, retrieve
                           the latest one used and set it as the image tag 
  --reuse-operator        Reuse an already installed operator
  --skip-spec-uninstall   Skip uninstallation after spec to allow post inspections
  --skip-operator-install  Skip installation of the operator
  --upgrade-from-previous-operator Upgrade from a previous version of the
                           operator
  --target-version        The supported target version used to create StackGres CR
  --k8s-version           The K8s version to use
  --previous-operator-version Set the previous version of the operator to test
  --pull-policy <policy>  Set the pull policy for the operator images
  --skip-load-operator    Skip loading of the operator images
  --enable-image-cache    Enable image cache
  --images-map <transformation> [<transformation> ...] A list of transformations that will be performed to loaded images.
                          Each transformation is a sed expression and a value separated by an equal \`=\`. If the image name
                          or substring of the image name that are loaded match the sed expression it will be replaced by the
                          transformation value. So that the resulting image name will be loaded instead and finally tagged as
                          the image name that was going to be loaded. For example:

                          postgres-util:v15\.0-build-6\.17=postgres-util:v15.0-hash-f9b66c957b5e1ff5b59f0310e9a23f38-linux-amd64

                          Will load image with name postgres-util:v15.0-hash-f9b66c957b5e1ff5b59f0310e9a23f38-linux-amd64
                          as it where the image with name postgres-util:v15.0-build-6.17.
                          
                          Requires 'push' and '--load-components' to work properly.
  --enable-pod-logging    Enable logging of pod resources
  --enable-event-logging  Enable logging of event resources
  --enable-snapshotting   Enable snapshotting of k8s events, pods and resources
  --enable-resource-logging Enable logging of resources by wathing them
  --load-components       Load component images
  --load-extensions       Load extension images
  --operator-public-registry <registry> The public image registry for the
                           operator that will be used during the installation
                           of the operator
  --components-public-registry <registry> The public image registry for the
                           components that will be used by the operator
  --operator-registry <registry> The image registry that will be used to pull
                           operator images
  --components-registry <registry> The image registry that will be used to
                           pull component images
  --extensions-registry <registry> The extension registry that will be used to
                           pull extensions images
  --extensions-repository-url <url> The extensions repository URL
  --cluster-parameters <parameters> Extra helm chart parameters for cluster
  --enable-cluster-resource-requirements Enable cluster resource requirements
  --k8s-cache-path <path> If not specified will be ~/.stackgres/kind-cache
  --disable-extensions-cache If set will disable extensions cache and use
                              remote repository directly.
  --use-current-build     If specified will use the current build instead of
                           the build inside the operator images.
                          
                          This feature may require to restart the k8s cluster.
  --local-build           When set will not push, check or pull build images to and from GitHub registry.
                           Still the build will require base images to be present locally or will try
                           to pull them from the respective remote repositories.
EOF
}

cli_completion() {
  cat << EOF
  alias e2e=$SHELL\ $E2E_PATH/e2e\ cli
EOF
  cat << EOF
  complete -F cli_do_completion e2e

  cli_do_completion() {
    if [ "x\${COMP_WORDS[\$((COMP_CWORD - 1))]}" != "x" ]
    then
      if [ "x\${COMP_WORDS[\$((COMP_CWORD - 1))]}" == "x--env" ]
      then
        COMPREPLY+=(\$(compgen -W '
          $(ls -1 "$E2E_PATH/envs" | tr '\n' ' ')
          ' -- "\${COMP_WORDS[\$COMP_CWORD]}"
          ))
        return
      fi
      if [[ " \${COMP_WORDS[*]} " =~ " test " ]] \
        && [[ " \${COMP_WORDS[*]} " =~ " -- " ]] \
        && [ "x\${COMP_WORDS[\$COMP_CWORD]#-}" = "x\${COMP_WORDS[\$COMP_CWORD]}" ]
      then
        compopt -o default
        COMPREPLY=()
        return
      fi
    fi
    if [ "x\${COMP_WORDS[\$COMP_CWORD]}" != "x" ]
    then
      if [ "x\${COMP_WORDS[\$COMP_CWORD]#-}" != "x\${COMP_WORDS[\$COMP_CWORD]}" ]
      then
        COMPREPLY+=(\$(compgen -W "
          \$(
            if [[ " \${COMP_WORDS[*]} " =~ " test " ]] \
              && {
                [[ ! " \${COMP_WORDS[*]} " =~ " -- " ]] \
                || [ "x\${COMP_WORDS[\$COMP_CWORD]}" = 'x--' ]
              }
            then
              printf %s --
            fi
          )
          --help
          --debug
          --use-cache
          --retry
          --timeout
          --parallelism
          --env
          --reset-env
          --native-image-tag
          --image-tag
          --use-latest-image-tag
          --reuse-operator
          --skip-spec-uninstall
          --skip-operator-install
          --upgrade-from-previous-operator
          --previous-operator-version
          --target-version
          --excluded-tests
          --pull-policy
          --skip-load-operator
          --enable-image-cache
          --images-map
          --enable-pods-logging
          --enable-event-logging
          --enable-resource-logging
          --load-components
          --load-extensions
          --operator-public-registry
          --components-public-registry
          --operator-registry
          --components-registry
          --extensions-registry
          --extensions-repository-url
          --cluster-parameters
          --enable-snapshotting
          --k8s-cache-path
          --disable-extensions-cache
          --use-current-build
          --local-build
          " -- "\${COMP_WORDS[\$COMP_CWORD]}"
          ))
        return
      fi
    fi
    COMPREPLY+=(\$(compgen -W "
      pull
      build
      push
      test
      \$(
        if [[ " \${COMP_WORDS[*]} " =~ " test " ]] \
          && [[ ! " \${COMP_WORDS[*]} " =~ " -- " ]]
        then
          printf %s --
        fi
      )
      " -- "\${COMP_WORDS[\$COMP_CWORD]}"
      ))
  }
EOF
}

cli() {
  local DO_BUILD=false DO_PUSH=false DO_PULL=false DO_TEST=false DO_PUSH_TEST=false

  local ARG VALUE POSITIONAL_SHIFTS=0

  cli_init_e2e_variables

  while [ "$#" -gt "$POSITIONAL_SHIFTS" ]
  do
    case "${1%%=*}" in
      ('--help')
      cli_help
      return
      ;;
      ('--debug')
      cli_set_parameter_boolean "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      if "$VALUE"
      then
        set -x
        SHELL_XTRACE=-x
      fi
      ;;
      ('--use-cache')
      cli_set_parameter_boolean "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      E2E_USE_TEST_CACHE="$VALUE"
      E2E_USE_TEST_CACHE_PER_TEST="$VALUE"
      ;;
      ('--env')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      E2E_ENV="$VALUE"
      ;;
      ('--reset-env')
      cli_set_parameter_boolean_negation "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      K8S_REUSE="$VALUE"
      ;;
      ('--retry')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      E2E_RETRY="$VALUE"
      ;;
      ('--timeout')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      E2E_TIMEOUT="$VALUE"
      ;;
      ('--parallelism')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      E2E_PARALLELISM="$VALUE"
      ;;
      ('--native-image-tag')
      cli_set_parameter_boolean "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      if "$VALUE"
      then
        IMAGE_TAG="${IMAGE_TAG%-jvm}"
      fi
      ;;
      ('--image-tag')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      if [ "${IMAGE_TAG%-jvm}" = "$IMAGE_TAG" ]
      then
        IMAGE_TAG="$VALUE"
      else
        IMAGE_TAG="$VALUE-jvm"
      fi
      ;;
      ('--use-latest-image-tag')
      cli_set_parameter_boolean "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      if "$VALUE"
      then
        local OPERATOR_IMAGE_NAME
        OPERATOR_IMAGE_NAME="$(sh stackgres-k8s/e2e/e2e eval 'printf %s "$OPERATOR_IMAGE_NAME"')"
        if ! sh stackgres-k8s/e2e/e2e has_image_k8s "$OPERATOR_IMAGE_NAME"
        then
          OPERATOR_IMAGE_NAME="$(sh stackgres-k8s/e2e/e2e get_latest_image_k8s "$OPERATOR_IMAGE_NAME")"
          IMAGE_TAG="${OPERATOR_IMAGE_NAME##*:}"
        fi
      fi
      ;;
      ('--reuse-operator')
      cli_set_parameter_boolean "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      E2E_REUSE_OPERATOR_PODS="$VALUE"
      ;;
      ('--skip-operator-install')
      cli_set_parameter_boolean "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      E2E_SKIP_OPERATOR_INSTALL="$VALUE"
      ;;
      ('--skip-spec-uninstall')
      cli_set_parameter_boolean "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      E2E_SKIP_SPEC_UNINSTALL="$VALUE"
      ;;
      ('--pull-policy')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      E2E_OPERATOR_PULL_POLICY="$VALUE"
      ;;
      ('--skip-load-operator')
      cli_set_parameter_boolean "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      E2E_SKIP_LOAD_OPERATOR="$VALUE"
      ;;
      ('--enable-snapshotting')
      cli_set_parameter_boolean_negation "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      E2E_DISABLE_LOGS_SNAPSHOTS="$VALUE"
      ;;
      ('--enable-image-cache')
      cli_set_parameter_boolean_negation "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      E2E_DISABLE_CACHE="$VALUE"
      ;;
      ('--images-map')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      E2E_IMAGE_MAP="$VALUE"
      ;;
      ('--enable-event-logging')
      cli_set_parameter_boolean_negation "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      E2E_DISABLE_LOGS="$VALUE"
      E2E_DISABLE_EVENT_LOGS="$VALUE"
      ;;
      ('--enable-pods-logging')
      cli_set_parameter_boolean_negation "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      E2E_DISABLE_LOGS="$VALUE"
      E2E_DISABLE_POD_LOGS="$VALUE"
      ;;
      ('--enable-resource-logging')
      cli_set_parameter_boolean_negation "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      E2E_DISABLE_LOGS="$VALUE"
      E2E_DISABLE_RESOURCE_LOGS="$VALUE"
      ;;
      ('--load-components')
      cli_set_parameter_boolean_negation "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      E2E_SKIP_LOAD_COMPONENTS="$VALUE"
      ;;
      ('--load-extensions')
      cli_set_parameter_boolean_negation "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      E2E_SKIP_LOAD_EXTENSIONS="$VALUE"
      ;;
      ('--operator-public-registry')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      OPERATOR_PUBLIC_REGISTRY="${VALUE%%/*}"
      OPERATOR_PUBLIC_REGISTRY_PATH="/${VALUE#*/}"
      ;;
      ('--components-public-registry')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      COMPONENT_PUBLIC_REGISTRY="${VALUE%%/*}"
      COMPONENT_PUBLIC_REGISTRY_PATH="/${VALUE#*/}"
      ;;
      ('--operator-registry')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      E2E_OPERATOR_REGISTRY="${VALUE%%/*}"
      E2E_OPERATOR_REGISTRY_PATH="/${VALUE#*/}"
      ;;
      ('--components-registry')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      E2E_COMPONENTS_REGISTRY="${VALUE%%/*}"
      E2E_COMPONENTS_REGISTRY_PATH="/${VALUE#*/}"
      ;;
      ('--extensions-registry')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      E2E_EXTENSIONS_REGISTRY="${VALUE%%/*}"
      E2E_EXTENSIONS_REGISTRY_PATH="/${VALUE#*/}"
      ;;
      ('--extensions-repository-url')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      EXTENSIONS_REPOSITORY_URL="$VALUE"
      ;;
      ('--k8s-cache-path')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      K8S_CACHE_PATH="$VALUE"
      ;;
      ('--disable-extensions-cache')
      cli_set_parameter_boolean_negation "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      EXTENSIONS_CACHE_ENABLED="$VALUE"
      ;;
      ('--use-current-build')
      cli_set_parameter_boolean "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      USE_CURRENT_BUILD="$VALUE"
      ;;
      ('--local-build')
      cli_set_parameter_boolean "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      SKIP_PUSH="$VALUE"
      SKIP_REMOTE_MANIFEST="$VALUE"
      ;;
      ('--cluster-parameters')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      E2E_CLUSTER_PARAMETERS="$VALUE"
      ;;
      ('--enable-cluster-resource-requirements')
      cli_set_parameter_boolean_negation "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      E2E_DISABLE_RESOURCE_REQUIREMENTS="$VALUE"
      ;;
      ('--upgrade-from-previous-operator')
      cli_set_parameter_boolean_negation "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      E2E_SKIP_UPGRADE_FROM_PREVIOUS_OPERATOR="$VALUE"
      ;;
      ('--target-version')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      STACKGRES_TARGET_VERSION="$VALUE"
      ;;
      ('--k8s-version')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      K8S_VERSION="$VALUE"
      ;;
      ('--previous-operator-version')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      STACKGRES_PREVIOUS_VERSION="$VALUE"
      ;;
      ('--excluded-tests')
      cli_set_parameter_value "$@"
      shift "$(cli_get_parameter_shifts "$@")"
      E2E_EXCLUDES="$VALUE"
      ;;
      ('pull')
      cli_set_parameter_boolean "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      DO_PULL="$VALUE"
      if [ "$DO_PULL" = true ]
      then
        E2E_FORCE_IMAGE_PULL=true
        E2E_SKIP_OPERATOR_LOAD=false
      fi
      ;;
      ('build')
      cli_set_parameter_boolean "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      DO_BUILD="$VALUE"
      if [ "$DO_BUILD" = true ]
      then
        E2E_SKIP_OPERATOR_LOAD=false
      fi
      ;;
      ('push')
      cli_set_parameter_boolean "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      DO_PUSH="$VALUE"
      ;;
      ('test')
      cli_set_parameter_boolean "$@"
      shift "$(cli_get_parameter_shifts_for_boolean "$@")"
      DO_TEST="$VALUE"
      ;;
      ('--')
      shift
      ;;
      ('--'*)
      >&2 echo "Unknown parameter $1"
      exit 1
      ;;
      (*)
      VALUE="$1"
      shift
      set -- "$@" "$VALUE"
      POSITIONAL_SHIFTS="$((POSITIONAL_SHIFTS+1))"
      ;;
    esac
  done

  cli_export_e2e_variables

  if "$DO_BUILD" && "$DO_PULL"
  then
    >&2 "build and pull are mutually exclusive"
    return 1
  fi
  if ! "$DO_TEST" \
     && ! "$DO_BUILD" \
     && ! "$DO_PUSH" \
     && ! "$DO_PULL"
  then
    cli_help
    return
  fi
  if [ "$USE_CURRENT_BUILD" = true ]
  then
    E2E_BUILD_ONLY_MODULES=true
    E2E_EXTRA_MOUNT_BUILD_PATH="$K8S_CACHE_PATH"/build
  fi
  if "$DO_BUILD"
  then
    E2E_BUILD_IMAGES=true E2E_PUSH_IMAGES=false "$SHELL" $SHELL_XTRACE "$E2E_PATH/e2e" setup_images
  fi
  if "$DO_PUSH"
  then
    E2E_BUILD_IMAGES=false E2E_PUSH_IMAGES=true "$SHELL" $SHELL_XTRACE "$E2E_PATH/e2e" setup_images
  fi
  if "$DO_TEST"
  then
    if [ "$E2E_USE_TEST_CACHE" != true ]
    then
      E2E_TEST_REGISTRY=
      E2E_TEST_REGISTRY_PATH=
    fi
    "$SHELL" $SHELL_XTRACE "$E2E_PATH/run-all-tests.sh" "$@"
  fi
}

cli_init_e2e_variables() {
  E2E_BUILD_IMAGES=false
  E2E_PUSH_IMAGES=false
  E2E_RETRY=1
  E2E_TIMEOUT=180
  E2E_PARALLELISM="$(nproc)"
  K8S_REUSE=true
  K8S_EXTRA_PORT=30100:9443:0.0.0.0:tcp
  K8S_USE_INTERNAL_REPOSITORY=true
  K8S_CACHE_PATH=~/".stackgres/${KIND_NAME:-kind}-cache"
  EXTENSIONS_CACHE_ENABLED=true
  EXTENSIONS_CACHE_HOST_PATH=/containerd-cache/extensions
  IMAGE_TAG="$(git rev-parse --short=8 HEAD)-jvm"
  E2E_FORCE_IMAGE_PULL=false
  E2E_SKIP_OPERATOR_LOAD=true
  E2E_SKIP_LOAD_OPERATOR=false
  E2E_SKIP_LOAD_COMPONENTS=true
  E2E_SKIP_LOAD_EXTENSIONS=true
  E2E_SKIP_UPGRADE_FROM_PREVIOUS_OPERATOR=true
  E2E_OPERATOR_OPTS="--set-string authentication.password=st4ckgr3s --set-string adminui.service.type=NodePort --set adminui.service.nodePort=30100 --set developer.enableJvmDebug=true $E2E_OPERATOR_OPTS"
  E2E_OPERATOR_BUNDLE_SGCONFIG='{"spec":{"authentication":{"password":"st4ckgr3s"},"adminui":{"service":{"type":"NodePort","nodePort":30100}},"developer":{"enableJvmDebug":true}}}'
  E2E_OPERATOR_BUNDLE_EXTRA_ENV_JSON='{"name":"DEBUG_OPERATOR","value":"true"},'
  E2E_DISABLE_CACHE=true
  E2E_DISABLE_LOGS=true
  E2E_DISABLE_POD_LOGS=true
  E2E_DISABLE_EVENT_LOGS=true
  E2E_DISABLE_RESOURCE_LOGS=true
  KIND_LOG=true
  KIND_LOG_RESOURCE=true
  E2E_OPERATOR_REGISTRY=registry.gitlab.com
  E2E_OPERATOR_REGISTRY_PATH=/ongresinc/stackgres/
  E2E_COMPONENTS_REGISTRY=registry.gitlab.com
  E2E_COMPONENTS_REGISTRY_PATH=/ongresinc/builder/oci/
  E2E_TEST_REGISTRY=registry.gitlab.com
  E2E_TEST_REGISTRY_PATH=ongresinc/stackgres
  E2E_DISABLE_LOGS_SNAPSHOTS=true
}

cli_export_e2e_variables() {
  export \
    E2E_PARALLELISM \
    E2E_RETRY \
    E2E_TIMEOUT \
    E2E_BUILD_IMAGES \
    E2E_PUSH_IMAGES \
    E2E_USE_TEST_CACHE \
    E2E_USE_TEST_CACHE_PER_TEST \
    E2E_ENV \
    K8S_REUSE \
    K8S_USE_INTERNAL_REPOSITORY \
    K8S_CACHE_PATH \
    EXTENSIONS_CACHE_ENABLED \
    SKIP_PUSH \
    SKIP_REMOTE_MANIFEST \
    EXTENSIONS_CACHE_HOST_PATH \
    E2E_EXTRA_MOUNT_BUILD_PATH \
    E2E_BUILD_ONLY_MODULES \
    K8S_EXTRA_PORT \
    E2E_OPERATOR_OPTS \
    E2E_OPERATOR_BUNDLE_SGCONFIG \
    E2E_OPERATOR_BUNDLE_EXTRA_ENV_JSON \
    E2E_DISABLE_CACHE \
    E2E_DISABLE_LOGS \
    E2E_DISABLE_POD_LOGS \
    E2E_DISABLE_RESOURCE_LOGS \
    E2E_DISABLE_RESOURCE_REQUIREMENTS \
    KIND_LOG \
    KIND_LOG_RESOURCE \
    IMAGE_TAG \
    E2E_REUSE_OPERATOR_PODS \
    E2E_SKIP_OPERATOR_INSTALL \
    E2E_SKIP_SPEC_UNINSTALL \
    E2E_OPERATOR_PULL_POLICY \
    E2E_FORCE_IMAGE_PULL \
    E2E_SKIP_OPERATOR_LOAD \
    E2E_SKIP_LOAD_OPERATOR \
    E2E_SKIP_LOAD_COMPONENTS \
    E2E_SKIP_LOAD_EXTENSIONS \
    OPERATOR_PUBLIC_REGISTRY OPERATOR_PUBLIC_REGISTRY_PATH \
    COMPONENT_PUBLIC_REGISTRY COMPONENT_PUBLIC_REGISTRY_PATH \
    E2E_OPERATOR_REGISTRY E2E_OPERATOR_REGISTRY_PATH \
    E2E_TEST_REGISTRY E2E_TEST_REGISTRY_PATH \
    E2E_COMPONENTS_REGISTRY E2E_COMPONENTS_REGISTRY_PATH \
    E2E_EXTENSIONS_REGISTRY E2E_EXTENSIONS_REGISTRY_PATH \
    EXTENSIONS_REPOSITORY_URL \
    E2E_CLUSTER_PARAMETERS \
    E2E_SKIP_UPGRADE_FROM_PREVIOUS_OPERATOR \
    STACKGRES_PREVIOUS_VERSION \
    STACKGRES_TARGET_VERSION \
    E2E_EXCLUDES \
    E2E_IMAGE_MAP \
    E2E_DISABLE_LOGS_SNAPSHOTS
}

cli_set_parameter_value() {
  if [ "${1%%=*}" = "$1" ]
  then
    shift
    VALUE="$1"
    shift
    if [ -z "$VALUE" ]
    then
      >&2 echo "You must specify a value for property ${1%%=*}"
      return 1
    fi
  else
    VALUE="${1#*=}"
    shift
  fi
}

cli_set_parameter_boolean() {
  if [ "${1%%=*}" = "$1" ]
  then
    shift
    VALUE="true"
  elif [ "${1%-}" != "$1" ]
  then
    shift
    VALUE="false"
  else
    if [ "${1#*=}" != true ] && [ "${1#*=}" != false ]
    then
      >&2 echo "You must specify true or false for property ${1%%=*}"
      return 1
    fi
    VALUE="${1#*=}"
    shift
  fi
}

cli_set_parameter_boolean_negation() {
  if [ "${1%%=*}" = "$1" ]
  then
    shift
    VALUE="false"
  elif [ "${1%-}" != "$1" ]
  then
    shift
    VALUE="true"
  else
    VALUE="${1#*=}"
    if [ "$VALUE" != true ] && [ "$VALUE" != false ]
    then
      >&2 echo "You must specify true or false for property ${1%%=*}"
      return 1
    fi
    if "$VALUE"
    then
      VALUE=false
    else
      VALUE=true
    fi
    shift
  fi
}

cli_get_parameter_shifts_for_boolean() {
  if [ "${1%%=*}" = "$1" ]
  then
    printf 1
  elif [ "${1%-}" != "$1" ]
  then
    printf 1
  else
    printf 2
  fi
}

cli_get_parameter_shifts() {
  if [ "${1%%=*}" = "$1" ]
  then
    printf 2
  elif [ "${1%-}" != "$1" ]
  then
    printf 1
  else
    printf 1
  fi
}
